﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using M3G;

namespace MyTestProject {

    public class TestTexture2D {
        [Fact]
        public void TestConstructor1 () {
            var img = new Image2D (Image2D.Luminance, 16, 16);
            var tex = new Texture2D (img);

            Assert.Equal (img, tex.Image);
            Assert.Equal (null, tex.DynamicImage);
            Assert.Equal (Texture2D.WrapRepeat, tex.WrappingS);
            Assert.Equal (Texture2D.WrapRepeat, tex.WrappingT);
            Assert.Equal (Texture2D.FilterBaseLevel, tex.LevelFilter);
            Assert.Equal (Texture2D.FilterNearest, tex.ImageFilter);
            Assert.Equal (Texture2D.FuncModulate, tex.Blending);
            Assert.Equal (0u, tex.BlendColor);
            Assert.Equal (null, tex.Combiner);
        }

        [Fact]
        public void TestConstructor2 () {
            var img = new DynamicImage2D ();
            var tex = new Texture2D (img);

            Assert.Equal (null, tex.Image);
            Assert.Equal (img, tex.DynamicImage);
            Assert.Equal (Texture2D.WrapRepeat, tex.WrappingS);
            Assert.Equal (Texture2D.WrapRepeat, tex.WrappingT);
            Assert.Equal (Texture2D.FuncModulate, tex.Blending);
            Assert.Equal (0u, tex.BlendColor);
            Assert.Equal (null, tex.Combiner);
        }

        [Fact]
        public void TestSetBlendColor () {
            var img = new Image2D (Image2D.Luminance, 16, 16);
            var tex = new Texture2D (img);

            tex.BlendColor = 0x87654321;

            Assert.Equal (0x87654321, tex.BlendColor);
        }

        [Fact]
        public void TestSetBlending () {
            var img = new Image2D (Image2D.Luminance, 16, 16);
            var tex = new Texture2D (img);

            tex.Blending = Texture2D.FuncDecal;

            Assert.Equal (Texture2D.FuncDecal, tex.Blending);
        }

        [Fact]
        public void TestSetCombiner () {
            var img = new Image2D (Image2D.Luminance, 16, 16);
            var tex = new Texture2D (img);
            var cmb = new TextureCombiner ();

            tex.SetCombiner (cmb);


            Assert.Equal (cmb, tex.Combiner);
        }


        [Fact]
        public void TestSetWrapping () {
            var img = new Image2D (Image2D.RGB, 16, 16);
            var tex = new Texture2D (img);

            tex.SetWrapping (Texture2D.WrapMirror, Texture2D.WrapClamp);

            Assert.Equal (Texture2D.WrapMirror, tex.WrappingS);
            Assert.Equal (Texture2D.WrapClamp, tex.WrappingT);
        }

        [Fact]
        public void TestSetImage () {
            var img1 = new Image2D (Image2D.RGB, 16, 16);
            var img2 = new Image2D (Image2D.LuminanceAlpha, 64, 64);
            var tex = new Texture2D (img1);

            tex.SetImage (img2);
            Assert.Equal (img2, tex.Image);
            Assert.Equal (null, tex.DynamicImage);
        }

        [Fact]
        public void TestSetDynamicImage () {
            var img1 = new Image2D (Image2D.RGB, 16, 16);
            var img2 = new DynamicImage2D ();
            var tex = new Texture2D (img1);

            tex.SetDynamicImage (img2);
            Assert.Equal (null, tex.Image);
            Assert.Equal (img2, tex.DynamicImage);
        }

        [Fact]
        public void TestReferences () {
            var img = new Image2D (Image2D.Luminance, 16, 16);
            var tex = new Texture2D (img);
            var cmb = new TextureCombiner ();
            tex.SetCombiner (cmb);

            var refs = tex.References;

            Assert.Equal (2, refs.Length);
            Assert.Equal (img, refs[0]);
            Assert.Equal (cmb, refs[1]);
        }

        [Fact]
        public void TestDuplicate () {
            var img = new Image2D (Image2D.Luminance, 16, 16);
            var tex = new Texture2D (img);
            var cmb = new TextureCombiner ();
            tex.BlendColor = 0x87654321;
            tex.Blending = Texture2D.FuncDecal;
            tex.SetCombiner (cmb);
            tex.SetWrapping (Texture2D.WrapMirror, Texture2D.WrapClamp);
            tex.UserID = 100;

            var dup = (Texture2D)tex.Duplicate ();

            Assert.Equal (tex.Image, dup.Image);
            Assert.Equal (tex.DynamicImage, dup.DynamicImage);
            Assert.Equal (tex.WrappingS, dup.WrappingS);
            Assert.Equal (tex.WrappingT, dup.WrappingT);
            Assert.Equal (tex.LevelFilter, dup.LevelFilter);
            Assert.Equal (tex.ImageFilter, dup.ImageFilter);
            Assert.Equal (tex.Blending, dup.Blending);
            Assert.Equal (tex.BlendColor, dup.BlendColor);
            Assert.Equal (tex.Combiner, dup.Combiner);
            Assert.Equal (tex.UserID, dup.UserID);

        }
    }


}
